package com.example.ch07_01_08spinner;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	private TextView show;
	private Spinner color;
	private ViewGroup rootLayout;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		initHendler();
	}

	private void initView() {
		// TODO Auto-generated method stub
		show = (TextView) this.findViewById(R.id.txt_show);
		color = (Spinner) this.findViewById(R.id.spn_color);
		rootLayout=(ViewGroup)this.findViewById(R.id.rootLayout);
		ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
				R.array.colors, android.R.layout.simple_spinner_item);
		// 設置下拉樣式
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// 為下拉清單設置配接器
		color.setAdapter(adapter);
		
	}

	private void initHendler() {
		// TODO Auto-generated method stub
		color.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				String message=arg0.getItemAtPosition(arg2).toString();
				Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
//				show.setTextColor(arg0.getItemAtPosition(arg2));
				if(message.equals("Red")){
					show.setTextColor(Color.RED);
					rootLayout.setBackgroundColor(Color.YELLOW);
				}else if(message.equals("Blue")){
					show.setTextColor(Color.BLUE);
					rootLayout.setBackgroundColor(Color.GREEN);
				}else{
					show.setTextColor(Color.BLACK);
					rootLayout.setBackgroundColor(Color.GRAY);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
